package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import flc.ast.AppConfig;
import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.ContentAdapter;
import flc.ast.databinding.ActivityGuideDetailBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.IntentUtil;
import stark.common.bean.StkResBean;

public class GuideDetailActivity extends BaseAc<ActivityGuideDetailBinding> {

    public static StkResBean stkResBean;
    private ContentAdapter contentAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_guide_detail;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivGuideDetailBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivDetailShare.setOnClickListener(this);

        mDataBinding.rvContentList.setLayoutManager(new LinearLayoutManager(mContext));
        contentAdapter = new ContentAdapter();
        mDataBinding.rvContentList.setAdapter(contentAdapter);
        contentAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivDetailShare://分享
                IntentUtil.shareText(mContext, stkResBean.getUrl());
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        GuideDetailActivity.stkResBean = contentAdapter.getItem(position);
        startActivity(GuideDetailActivity.class);
    }

    @Override
    protected void initData() {
        getData();
        mDataBinding.webView.getSettings().setJavaScriptEnabled(true);
        // 注入JavaScript函数来改变文字颜色
        String jsCode = "javascript:document.body.style.setProperty('color', 'rgba(255, 255, 255,1)');";
        // 在WebView加载完成后注入JavaScript代码
        mDataBinding.webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                mDataBinding.webView.evaluateJavascript(jsCode, null);
            }
        });
        mDataBinding.webView.setBackgroundColor(Color.TRANSPARENT);
        mDataBinding.webView.loadUrl(stkResBean.getUrl());
    }

    private void getData() {
        StkResApi.getTagResourceList(this, AppConfig.CONTENT_DATA, StkResApi.createParamMap(0, 6), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        contentAdapter.setList(result);
                    }
                }
            }
        });
    }

}